/**
 * 
 */
package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *Produce data files for R-tree indexing.<br>
 * Input File format: id time x y <br>
 * 
 * Out file format: op id x1 y1 x2 y2
 * 
 * @author Xiaohui
 * 
 */
public class MyFormat {

	public void genRTreeFile(String infile, String outfile, String delim) {
		BufferedReader in;
		BufferedWriter out;

		try {
			in = new BufferedReader(new FileReader(infile));
			out = new BufferedWriter(new FileWriter(outfile));
			String str;
			String a = null;
			// loop over all comment lines
			while ((str = in.readLine()) != null) {
				if (!str.startsWith("#")) {
					a = str;
					break;
				}
			}

			while ((str = in.readLine()) != null) {
				StringTokenizer st2 = new StringTokenizer(str, delim);
				StringTokenizer st1 = new StringTokenizer(a, delim);
				String temp = "1" + delim; // insertion
				temp += st1.nextToken() + delim; // id
				st1.nextElement(); // time
				st2.nextElement(); // id
				st2.nextElement(); // time

				temp += st1.nextToken() + delim; // x1
				temp += st1.nextToken() + delim; // y1
				temp += st2.nextToken() + delim; // x2
				temp += st2.nextToken() + delim; // y2
				out.write(temp);
				out.newLine();
				a = str; // update a
			}
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyFormat f = new MyFormat();
		String in = "D:/Research/trajectory indexing/temp/tt.txt";
		String out = "D:/Research/trajectory indexing/temp/tt1.txt";
		f.genRTreeFile(in, out, "\t");
	}

}
